<script>
import { NavBar, Tab, Tabs, Empty, List } from "vant";
import { getMenu, getWorks, updateInfo } from "@/api/mine";

export default {
  components: {
    [NavBar.name]: NavBar,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [Empty.name]: Empty,
    [List.name]: List,
  },
  data() {
    return {
      // 所在列表
      active: 0,

      // 菜谱数据及状态
      list1: [],
      length1: 0,
      loading1: false,
      loading2: false,

      // 作品数据及状态
      list2: [],
      length2: 0,
      finished1: false,
      finished2: false,
    };
  },
  computed: {
    // 用户信息
    user() {
      return this.$store.getters["user/user"];
    },
  },
  methods: {
    //  获取菜谱数据
    async menu() {
      try {
        let res = await getMenu({
          limit: 20,
          offset: this.list1.length,
        });
        this.length1 = res.data.length;
        if (this.length1 != this.list1.length) {
          Array.from(res.data.data).forEach((item) => {
            this.list1.push(item);
          });
          console.log(this.list1);
          this.loading1 = false;
        } else {
          this.loading1 = false;
          this.finished1 = true;
        }
      } catch (error) {}
    },
    // 获取作品数据
    async works() {
      let res = await getWorks({
        limit: 20,
        offset: this.list2.length,
      });
      this.length2 = res.data.length;
      if (this.length2 != this.list2.length) {
        Array.from(res.data.data).forEach((item) => {
          this.list2.push(item);
        });
        this.loading2 = false;
      } else {
        this.loading2 = false;
        this.finished2 = true;
      }
    },
  },
  created() {
    this.menu();
    this.works();
  },
};
</script>
<template>
  <div>
    <div class="page">
      <van-nav-bar title="个人中心" fixed />
      <div class="con_wrap">
        <div class="personalInformation">
          <div class="userinformation">
            <router-link :to="{ name: 'Useravatar' }" class="headPortrait">
              <img :src="user.avatar" alt="" />
            </router-link>
            <div class="information">
              <h2>
                {{ user.user_name }}
                <router-link :to="{ name: 'set' }"
                  ><i class="iconfont icon-shezhi-xianxing"></i
                ></router-link>
              </h2>
              <p>
                <span>{{ user.sex }}</span
                ><span>{{ user.registerDate }}年加入</span>
              </p>
            </div>
          </div>
          <div class="otherInformation">
            <router-link :to="{ name: 'fabulous' }">
              <h2>{{ user.concern }}</h2>
              <p>关注</p>
            </router-link>
            <span></span>
            <router-link :to="{ name: 'fabulous' }">
              <h2>{{ user.fans }}</h2>
              <p>粉丝</p>
            </router-link>
            <span></span>
            <router-link :to="{ name: 'follow' }">
              <h2>{{ user.likenum }}</h2>
              <p>收到的点赞</p>
            </router-link>
          </div>
        </div>

        <van-tabs
          v-model="active"
          swipeable
          background="transparent"
          class="tabs"
        >
          <van-tab :title="'菜谱 ' + length1">
            <van-list
              v-model="loading1"
              :finished="finished1"
              finished-text="没有更多了"
              @load="menu"
            >
              <van-empty
                class="custom-image"
                image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
                description="无菜谱"
                v-if="list1.length == 0"
              />

              <div class="menu_wrap" v-else>
                <router-link
                  :to="{ name: 'detail', params: { recipe_id: item._id } }"
                  class="menu_box"
                  v-for="item in list1"
                  :key="item._id"
                >
                  <img v-lazy="item.coverImg" alt="" v-if="item.type == 1" />
                  <video :src="item.video" v-else></video>
                  <p class="van-ellipsis">{{ item.title }}</p>
                </router-link>
              </div>
            </van-list>
          </van-tab>

          <van-tab :title="'作品 ' + length2">
            <van-list
              v-model="loading2"
              :finished="finished2"
              finished-text="没有更多了"
              @load="works"
            >
              <van-empty
                class="custom-image"
                image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
                description="无作品"
                v-if="list2.length == 0"
              />
              <div class="works_wrap" v-else>
                <router-link
                  :to="{ name: 'works', params: { works_id: item._id } }"
                  class="works_box"
                  v-for="item in list2"
                  :key="item._id"
                >
                  <img v-lazy="item.img[0]" alt="" />
                </router-link>
              </div>
            </van-list>
          </van-tab>
        </van-tabs>
      </div>
    </div>
    <transition name="van-slide-right">
      <router-view></router-view>
    </transition>
  </div>
</template>

<style lang="scss" scoped>
.page {
  overflow: hidden;
}
.con_wrap {
  height: calc(100% - 50px);
  width: 100%;
  background: #fff;
  padding-top: 8vw;
  overflow-y: auto;
  overflow-x: hidden;
  .personalInformation {
    box-shadow: 0 0 10.6667vw 5.3333vw #f0f1f4;
    margin: 0 5vw;
    margin-top: 5.3333vw;
    margin-bottom: 4vw;
    padding: 0 4vw;
    background: #ffffff;
    border-radius: 2.6667vw;
    .userinformation {
      padding: 5.3333vw 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      position: relative;

      .headPortrait {
        transform: translateY(-20%);
        border-radius: 100%;
        width: 24vw;
        height: 24vw;
        padding: 1.3333vw;
        background: #fff;
        box-shadow: 0 0 4vw 0.5333vw #ccc;
        position: absolute;

        img {
          border-radius: 100%;
          width: 100%;
          height: 100%;
        }
      }
      .information {
        flex: 1;
        margin-left: 32vw;
        h2 {
          font-size: 4.8vw;
          font-weight: 800;
          margin-bottom: 5.3333vw;
          display: flex;
          justify-content: space-between;
          align-items: center;
          a {
            font-weight: 500;
            color: #000;
            i {
              font-size: 6.6667vw;
            }
          }
        }
        p {
          font-size: 3.2vw;
          color: #8d92a3;
          span:first-of-type {
            margin-right: 1.3333vw;
          }
        }
      }
    }
    .otherInformation {
      padding: 5.3333vw 0;
      display: flex;
      justify-content: space-around;
      align-items: center;
      span {
        width: 0.2667vw;
        height: 4vw;
        background: #ccc;
      }
      a {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        h2 {
          margin-bottom: 2vw;
          color: #000;
          font-size: 4.2667vw;
        }
        p {
          font-size: 2.6667vw;
          color: #8d92a3;
        }
      }
    }
  }
  .tabs {
    padding-bottom: 80px;
    .menu_wrap {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      margin: 0 5vw;
      margin-top: 5.3333vw;

      .menu_box {
        flex-shrink: 0;
        width: 43vw;
        height: 65vw;
        img {
          width: 100%;
          height: 85%;
          border-radius: 2.6667vw;
        }
        video {
          width: 100%;
          height: 85%;
          border-radius: 2.6667vw;
        }
        p {
          margin-top: 2.6667vw;
          font-size: 4.2667vw;
          color: #000;
        }
      }
    }
    .works_wrap {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      margin: 0 1vw;
      margin-top: 5.3333vw;
      .works_box {
        margin-top: 2.6667vw;
        flex-shrink: 0;
        width: 32vw;
        height: 35vw;
        img {
          width: 100%;
          height: 100%;
          border-radius: 1.3333vw;
        }
      }
    }
  }
}
</style>
